﻿$(function () {
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();
    window.selectOption = new SelectOption();
    window.activityItem = new ActivityItem("activityItem", "#ItemHTML", "#LinkDiv .AIcon div.Icon", "LinkDiv", "<div class=\"ItemDiv\">", "</div>", null, null, true);
    $("#ButtonSave").button({ icons: { primary: "ui-icon-disk"} }).click(function () {
        if (CheckWebuBttonList()) {
            var TempJson = [[{ "OperationKey": "修改产品资料", "Parameters": { "@text_description": $("#text_description").val(), "@remark": $("#remark").val(), "@web_desc": $("#web_desc").val(), "@web_keyword": $("#web_keyword").val(), "@web_spec": $("#web_spec").val(), "@web_description": editor.getData(), "@web_button_list": GetWebuBttonList()}}]];
            SetFileActionObj(TempJson);
            SetImageActionObj(TempJson);
            var JsonSend = { "ActionParameters": JSON.stringify(TempJson) };
            window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.location.href = JsonOriginal.UrlReferrer;", "", true, false);
        }
    });
    $("#ButtonBack").button({ icons: { primary: "ui-icon-arrowreturnthick-1-w"} }).click(function () { window.location.href = JsonOriginal.UrlReferrer; });

    window.tabClass = new TabClass("tabClass", "TabRadio", ["radio1", "radio2", "radio3", "radio4", "radio5"], ["TabTR1", "TabTR2", "TabTR3", "TabTR4", "TabTR5"], [null, null, SetImageUpdate, SetFileUpdate, null]);

    for (var i = 0; i < WebButtonList.length; i++) {
        window.activityItem.AddItem();
        $("#LinkDiv div.ItemDiv:last").find("input[name='ItemType']").val(WebButtonList[i].ItemType);
        $("#LinkDiv div.ItemDiv:last").find("input[name='ItemName']").val(WebButtonList[i].ItemName);
        $("#LinkDiv div.ItemDiv:last").find("input[name='ItemValue']").val(WebButtonList[i].ItemValue);
        if (!WebButtonList[i].ItemEnable)
            $("#LinkDiv div.ItemDiv:last").find("input[name='ItemEnable']").removeAttr("checked");
    }

    if (HasntPermission("修改产品资料")) {
        $("#ButtonSave").button({ disabled: true }); $("#MainTable input, #MainTable textarea").attr("readonly", "readonly");
        $("#LinkDiv .Icon").css("visibility", "hidden"); $("#LinkDiv input[type=checkbox]").attr("disabled", "disabled"); $("#LinkDiv input").removeClass("Select").attr("readonly", "readonly");
        $("#LinkDiv").html("<table></table>");
    } else {
        window.activityItem.AddItem();
    }

    window.editor = CKEDITOR.replace("web_description", {
        removePlugins: 'resize',
        toolbar: 'MyToolbar',
        height: 300,
        width: 904
    });
    CKFinder.setupCKEditor(editor, "/Scripts/CKFinder/");
    window.uploadifyClass = null;
});

/* 格式可活动项目 --------------------------------------------------------------------------------------------*/

function GetWebuBttonList() {
    var R = new Array();
    $("#LinkDiv div.ItemDiv").each(function (index, element) {
        if ($(element).find("input[name='ItemType']").val() != "") {
            R.push({ "ItemType": $(element).find("input[name='ItemType']").val(), "ItemName": $(element).find("input[name='ItemName']").val(), "ItemValue": $(element).find("input[name='ItemValue']").val(), "ItemEnable": $(element).find("input[name='ItemEnable']").is(':checked') });
        } 
    });
    return JSON.stringify(R);
}

function CheckWebuBttonList() {
    var R = true;
    $("#LinkDiv div.ItemDiv").each(function (index, element) {
        if (R) {
            if ($(element).find("input[name='ItemType']").val() == "" && ($(element).find("input[name='ItemName']").val() != "" || $(element).find("input[name='ItemValue']").val() != "")) {
                infoDialog.OpenDialog("链接类别必须与链接名称或链接地址同时填写！", "Alert");
                window.tabClass.Show(4);
                R = false;
            }
        }
    });
    return R;
}

/* 文档上传 --------------------------------------------------------------------------------------------*/

function SetFileUpdate(index) {
    if (window.activityFile == null) {
        if (HasPermission("修改产品资料")) {
            var JsonObj = JSON.parse(JsonOriginal.Operations["上传产品文档"].JsonParameter);
            window.uploadifyClass = new UploadifyClass("DocUpload", JsonOriginal.UrlUploadify, "上传文档", "文档文件", JsonObj["文件后序"], "90MB", { "ActionParameters": JSON.stringify([[{ "OperationKey": "上传产品文档"}]]) }, FileUploadSuccess);
            $("#UploadDoc_Panel").css("display", "block").removeAttr("style");
        }        
        var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "获取产品文档"}]]) };
        window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.activityFile = new ActivityItem(\"activityFile\", \"#FileHTML\", null, \"FileDiv\", \"<div class=\\\"FileDiv\\\">\", \"</div>\", null, null, false);SetFiles(data.ReturnData[0]);window.tabClass.Show(" + index + ");", "window.tabClass.Show(0);$(\"label[for='radio1']\").click();", false, true);
    } else {
        window.tabClass.Show(index);
    }
}
function SetFileActionObj(TempJson) {
    $("#FileDiv .FileDiv").each(function (index, element) {
        if ($(element).hasClass("DeleteFileDiv")) {
            TempJson.push([{ "OperationKey": "删除产品文档", "JsonParameter": JSON.stringify({ "内部标识": $(element).find("input[name='ItemType']").val() })}]);
        } else if ($(element).find("input[name='ItemName']").val() != $(element).find("input[name='ItemName']").attr("title") || ($(element).find("input[name='ItemEnable']").is(':checked') != ($(element).find("input[name='ItemType']").val().indexOf("^OL") > -1))) {
            TempJson.push([{ "OperationKey": "修改产品文档", "JsonParameter": JSON.stringify({ "内部标识": $(element).find("input[name='ItemType']").val(), "文件名称": $(element).find("input[name='ItemName']").val(), "外部下载": $(element).find("input[name='ItemEnable']").is(':checked') })}]);
        }
    });
}
function SetFiles(Data) {
    if (Data != null) {
        for (var i = 0; i < Data.length; i++) {
            Filename = Data[i].substr(Data[i].indexOf("-") + 1, Data[i].lastIndexOf(".") - Data[i].indexOf("-") - 1);
            AddFile(Data[i], Filename, (Data[i].indexOf("^OL") > -1));
        }
        if (HasntPermission("修改产品资料")) { $("#FileDiv .DeleteFile").css("visibility", "hidden"); $("#FileDiv input[type=checkbox]").attr("disabled", "disabled"); $("#FileDiv input").removeClass("Required").attr("readonly", "readonly"); }
    }
}
function AddFile(ItemType, ItemName, ItemEnable) {
    UPDT = ItemType.substr(ItemType.indexOf("_") + 1, 15);
    UPDT = "20" + UPDT.substr(0, 1) + UPDT.substr(1, 1) + "-" + UPDT.substr(2, 1) + UPDT.substr(3, 1) + "-" + UPDT.substr(4, 1) + UPDT.substr(5, 1) + " " + UPDT.substr(6, 1) + UPDT.substr(7, 1) + ":" + UPDT.substr(8, 1) + UPDT.substr(9, 1) + ":" + UPDT.substr(10, 1) + UPDT.substr(11, 1) + "." + UPDT.substr(12, 1) + UPDT.substr(13, 1) + UPDT.substr(14, 1);
    var DownUrl = JsonOriginal.UrlDown + "&";
    if (ItemType.indexOf("^OL") > 0)
        DownUrl = JsonOriginal.UrlDown.substr(0, DownUrl.lastIndexOf("?") + 1);
    window.activityFile.AddItem();
    $("#FileDiv div.FileDiv:last").find("input[name='ItemType']").val(ItemType);
    $("#FileDiv div.FileDiv:last").find("input[name='ItemTime']").val(UPDT);
    $("#FileDiv div.FileDiv:last").find("input[name='ItemTime']").attr("title", UPDT);
    $("#FileDiv div.FileDiv:last").find("input[name='ItemName']").val(ItemName);
    $("#FileDiv div.FileDiv:last").find("input[name='ItemName']").attr("title", ItemName);
    $("#FileDiv div.FileDiv:last").find(".DownLoad a").attr("title", "下载文件 " + ItemName + ItemType.substring(ItemType.indexOf(".")));
    if (HasntPermission("下载产品文档") && ItemType.indexOf("^OL") == -1) {
        $("#FileDiv div.FileDiv:last").find(".DownLoad").css("visibility", "hidden");
    } else {
        $("#FileDiv div.FileDiv:last").find(".DownLoad a").attr("href", DownUrl + "FileName=" + encodeURIComponent(ItemType) + "&OperationKey=" + encodeURIComponent("下载产品文档"));
    }
    if (ItemEnable) {
        $("#FileDiv div.FileDiv:last").find("input[name='ItemEnable']").attr("checked", "checked");
    }
}
function FileUploadSuccess(file, data, response) {
    var Filename = file.name.substr(0, file.name.lastIndexOf("."));
    AddFile(data, Filename, false);
}
function DeleteFile(EventObj) {
    $(EventObj).parent().parent().toggleClass("DeleteFileDiv");
}

/* 图片上传 --------------------------------------------------------------------------------------------*/

function SetImageUpdate(index) {
    if (window.activityImage == null) {
        if (HasPermission("修改产品资料")) {
            var JsonObj = JSON.parse(JsonOriginal.Operations["上传产品图片"].JsonParameter);
            window.uploadifyClass = new UploadifyClass("ImageUpload", JsonOriginal.UrlUploadify, "上传图片", "上传图片", JsonObj["文件后序"], "10MB", { "ActionParameters": JSON.stringify([[{ "OperationKey": "上传产品图片"}]]) }, ImageUploadSuccess);
            $("#UploadImage_Panel").removeAttr("style");
        }
        var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": "获取图片数据"}], [{ "OperationKey": "获取产品图片"}]]) };
        window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.activityImage = new ActivityItem(\"activityImage\", \"#ImageHTML\", null, \"ImageDiv\", \"<div class=\\\"ImageDiv\\\" style=\\\"float:left;\\\">\", \"</div>\", null, null, false);SetImages(data.ReturnData);window.tabClass.Show(" + index + ");", "window.tabClass.Show(0);$(\"label[for='radio1']\").click();", false, true);
    } else {
        window.tabClass.Show(index);
    }
}
function SetImageActionObj(TempJson) {
    $("#ImageDiv .ImageDiv").each(function (index, element) {
        if ($(element).hasClass("DeleteImageDiv")) {
            if ($(element).find("input[name='AutoID']").val() != -1) {
                TempJson.push([{ "OperationKey": "删除图片数据", "Parameters": { "@AutoID": $(element).find("input[name='AutoID']").val()}}]);
            }
            TempJson.push([{ "OperationKey": "删除产品图片", "JsonParameter": JSON.stringify({ "内部标识": $(element).find("input[name='FileName']").val() })}]);
        } else if ($(element).find("input[name='AutoID']").val() == -1) {
            TempJson.push([{ "OperationKey": "添加图片数据", "Parameters": { "@ImageName": $(element).find("input[name='FileName']").val(), "@Summary": $(element).find("input[name='Summary']").val(), "@ListOrder": index}}]);
        } else {
            TempJson.push([{ "OperationKey": "修改图片数据", "Parameters": { "@AutoID": $(element).find("input[name='AutoID']").val(), "@Summary": $(element).find("input[name='Summary']").val(), "@ListOrder": index}}]);
        }
    });
}
function SetImages(Data) {
    if (Data[0] != null) {
        for (var i = 0; i < Data[0].length; i++) {
            AddImage(Data[0][i]["AutoID"], Data[0][i]["ImageName"].substring(0,Data[0][i]["ImageName"].lastIndexOf(".")), Data[0][i]["ImageName"].substring(Data[0][i]["ImageName"].lastIndexOf(".")), Data[0][i]["Summary"]);
        }
    }
    if (Data[1] != null) {
        for (var i = 0; i < Data[1].length; i++) {
            if (Data[1][i].indexOf("-") == -1) {
                if ($("#ImageDiv div.ImageDiv input[value='" + Data[1][i] + "']").size() == 0) {
                    AddImage(-1, Data[1][i].substring(0, Data[1][i].lastIndexOf(".")), Data[1][i].substring(Data[1][i].lastIndexOf(".")), "");
                }
            }
        }
    }
    if (HasntPermission("修改产品资料")) { $("#ImageDiv .Forward, #ImageDiv .Backward, #ImageDiv .ToDelete").css("visibility", "hidden"); $("#ImageDiv input").attr("readonly", "readonly"); }
}
function AddImage(AutoID, FileName, Expand, Summary) {
    var JsonObj = JSON.parse(JsonOriginal.Operations["获取产品图片"].JsonParameter);
    window.activityImage.AddItem();
    $("#ImageDiv div.ImageDiv:last").find("input[name='AutoID']").val(AutoID);
    $("#ImageDiv div.ImageDiv:last").find("input[name='FileName']").val(FileName + Expand);
    $("#ImageDiv div.ImageDiv:last").find("img").attr("src", "/UpFiles/Images/" + JsonObj["基目录"] + "/" + JsonOriginal.KeyList["KeyB"] + "/" + FileName + "-Expand.jpg");
    $("#ImageDiv div.ImageDiv:last").find(".Thumbnail a").attr("href", "/UpFiles/Images/" + JsonObj["基目录"] + "/" + JsonOriginal.KeyList["KeyB"] + "/" + FileName + "-Expand.jpg");
    $("#ImageDiv div.ImageDiv:last").find(".DownLoad a").attr("href", "/UpFiles/Images/" + JsonObj["基目录"] + "/" + JsonOriginal.KeyList["KeyB"] + "/" + FileName + Expand);
    $("#ImageDiv div.ImageDiv:last").find("input[name='Summary']").val(Summary);
    $("#ImageDiv div.ImageDiv:last").find("input[name='Summary']").attr("title", Summary);
}
function ImageUploadSuccess(file, data, response) {
    AddImage(-1, data, file.type, "");
}
function DeleteImage(EventObj) {
    $(EventObj).parent().parent().parent().parent().parent().parent().parent().toggleClass("DeleteImageDiv");
}
function Forward(EventObj) {
    var Self = $(EventObj).parent().parent().parent().parent().parent().parent().parent();
    Self.prev().before(Self);
}
function Backward(EventObj) {
    var Self = $(EventObj).parent().parent().parent().parent().parent().parent().parent();
    Self.next().after(Self);
}
